

package net.jjjshop.job.scheduled;

import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.cache.RedisUtil;
import net.jjjshop.common.constants.TaskConstants;
import net.jjjshop.common.service.lbt.BuyProductSettlementService;
import net.jjjshop.common.service.lbt.LbLineClassService;
import net.jjjshop.common.service.lbt.LbTransferRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 订单定时任务、未付款关闭，自动确认收货，订单结算
 */

@Slf4j
@Component
@Configuration //读取配置
public class Buy2Scheduled {
    @Autowired
    private BuyProductSettlementService buyProductSettlementService;
    @Autowired
    private RedisUtil redisUtil;

    private static final Logger logger = LoggerFactory.getLogger(Buy2Scheduled.class);
    @Autowired
    private LbLineClassService classService;
    @Autowired
    private LbTransferRecordService lbTransferRecordService;
    /**
     * 每分钟执行一次
     */
//    @Scheduled(fixedRate = 10000)  //每10秒执行一次
    @Scheduled(cron = "0/3 * * * * ? ")
    public void init()  {
//        System.out.println("10秒执行一次");
        String redisKey = TaskConstants.ORDER_TASK_REDIS_KEY_AFTER_TAKE_BY_USER;
        Long size = redisUtil.getListSize(redisKey);
        if (size < 1) {
            return;
        }
        Object id = redisUtil.getRightPop(redisKey, 10L);
        try {
            Boolean result = buyProductSettlementService.orderReceiving(Integer.valueOf(id.toString()));
            if (!result) {
                redisUtil.lPush(redisKey, id);
                logger.info("订单支付订单任务异常，result异常，订单号id为: " + id);
            }
            if (result){
                logger.info("订单支付订单任务完成，result返回true，订单号id为: " + id);
            }
        } catch (Exception e) {
            redisUtil.lPush(redisKey, id);
            logger.info("订单支付订单任务异常，catch打印，订单号id为: " + id);
        }
    }

    /**
     * 结算权益卡
     */
    @Scheduled(cron = "0 20 0 * * ? ")
    public void settlement()  {
        classService.freeTzNum();
    }

    /**
     * 结算权益卡价格
     */
    @Scheduled(cron = "0 15 0 * * ? ")
    public void countTz()  {
        lbTransferRecordService.calculateTzPrice(null);
    }

    /**
     * 计算懂事加权分红
     */
    @Scheduled(cron = "0 10 0 * * ? ")
    public void SensibleSettlement()  {
        buyProductSettlementService.SensibleSettlement();
    }
}
